package yaty.lee.ssh2.util;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonUtil {
	private static final JsonConfig config = initConfig();

	public static synchronized JsonConfig config() {
		return config;
	}

	private static JsonConfig initConfig() {
		JsonConfig config = new JsonConfig();
		JsonValueProcessor p1 = new JsonDateProcessor();
		config.registerJsonValueProcessor(java.sql.Date.class, p1);
		config.registerJsonValueProcessor(java.util.Date.class, p1);
		return config;
	}

	public static class JsonDateProcessor implements JsonValueProcessor {

		@Override
		public Object processArrayValue(Object obj, JsonConfig arg1) {
			java.util.Date date = null;
			if (obj instanceof java.sql.Date) {
				date = new java.util.Date(((java.sql.Date) obj).getTime());
			} else {
				date = (java.util.Date) obj;
			}
			return DateUtil.format(date.getTime(), "yyyy-MM-dd");
		}

		@Override
		public Object processObjectValue(String key, Object obj,
				JsonConfig config) {
			return processArrayValue(obj, config);
		}

	}
}
